import React, { Component } from "react";
import { Consumer } from "./context";
class Check extends Component {
    render() {
        let { content, flg, typeIndex, listIndex } = this.props;
        return <Consumer>
            {
                (store) => {
                    return <div className="checks">
                        <span onClick={() => {
                            this.selectClick(store, flg, typeIndex, listIndex)
                        }} style={flg ? {
                            backgroundImage: `url(${require("./img/选中.png")})`,
                            backgroundSize: "100% 100%"
                        } : null}></span>
                        <p>{content}</p>
                    </div>
                }
            }
        </Consumer>
    }
    selectClick = (obj, flg, typeIndex, listIndex) => {
        flg = !flg;
        if (typeIndex === -1 && listIndex === -1) {
            obj.data.allFlg = flg;
            obj.data.allPrice =0;
            obj.data.group.forEach((typeItem) => {
                if (flg) {
                    typeItem.typeFlg=true;
                    typeItem.list.forEach((item) => {
                        item.listFlg = true;
                        obj.data.allPrice += item.num * item.price;
                    })
                } else {
                    typeItem.typeFlg=false;
                    typeItem.list.forEach((item) => {
                        item.listFlg = false;
                        obj.data.allPrice =0;
                    })
                }


            })

        } else if (typeIndex > -1 && listIndex === -1) {
            obj.data.group[typeIndex].typeFlg = flg;
            obj.data.group[typeIndex].list.forEach((item) => {
                if (flg) {
                    item.listFlg = true;
                    obj.data.allPrice += item.num * item.price;
                } else {
                    item.listFlg = false;
                    obj.data.allPrice -= item.num * item.price;
                }
            })

        } else if (typeIndex > -1 && listIndex > -1) {
            obj.data.group[typeIndex].list[listIndex].listFlg = flg;

            let item = obj.data.group[typeIndex].list[listIndex];

            let flgs = obj.data.group[typeIndex].list.every((item) => {
                return item.listFlg;
            })
           
            if (flgs) {
                obj.data.group[typeIndex].typeFlg = true;
           
            } else {
                obj.data.group[typeIndex].typeFlg = false;
                obj.data.allFlg = false;
            }
           

            if (flg) {
                obj.data.allPrice += item.num * item.price;
            } else {
                obj.data.allPrice -= item.num * item.price;
            }

        }
        let flgss=obj.data.group.every((item) => {
            return item.typeFlg;
        })
        if(flgss){
            obj.data.allFlg = true;
        }else{
            obj.data.allFlg = false;
        }

        obj.change(obj.data);

    }
}
export default Check;